import { getUserPhone } from '@src/api/base'
import { getBaseInfo } from '@src/api/member'
import { submitLoanMsg } from '@src/api/loan'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import loanMove from '../../store/loanMove'

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: loanMove,
      fields: ['shareMsg'],
      actions: ['setSharMsg'],
    },
  ],
  data: {
    userInfo:{},
    urlMsg:{},
    formMsg:{
      carName:'',
      userPhone:'',
      userName:''
    },
    codeStatus:''
  },
  methods: {
    onLogin() {
      this.setOpenid()
      let _that = this
      wx.checkSession({
        success() {},
        fail() {
          _that.selectComponent('#login')._login(true)
        },
      })
    },
    onReady() {},
    onAnon() {
      // this.updateStoreBindings();
      this.setData({ login: false })
    },
    onLoad(options) {
      if (options.openId) {
        console.log(options)
        let {tweeterPhone,openId} = options
        this.setData({ urlMsg: options })
        this.setSharMsg({ tweeterPhone,openId })
      }
      wx.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage', 'shareTimeline'],
      })
    },
    // 分享
    onShareAppMessage(e) {
      return {
        title: '给你分享一个好东西',
        imageUrl: '',
        path: `pages/loanMove/pages/index/main?openId=${this.data.userInfo.openId}&tweeterPhone=${this.data.userInfo.phone}`,
        success: (res) => {},
      }
    },
    // 表单提交事件
    submitData() {
      let {businessId} = getApp().globalData.extConfig;
      let{formMsg:{userPhone,userName,carName},urlMsg:{tweeterPhone,openId},userInfo:{openId:userOpenid},codeStatus,phone} = this.data
      let rep = /^1[35789]\d{9}$/
      if(!carName) {wx.showToast({title:'请输入意向车型',icon:'none'}); return}
      if(!userName) {wx.showToast({title:'请输入您的姓名',icon:'none'}); return}
      if(!userPhone) {wx.showToast({title:'请输入手机号',icon:'none'}); return}
      if(!rep.test(userPhone)){wx.showToast({title:'手机号格式有误',icon:'none'}); return}
      wx.showModal({
        content: '确认提交信息？',
        success:(res) =>{
          if (res.confirm) {
            let params = {
              businessId,
              userPhone,
              userName,
              carName,
            }
            if(codeStatus == 0){
              params.openId = userOpenid
              params.tweeterPhone = phone
            }else{
              params.openId = openId?openId:userOpenid
              params.tweeterPhone = openId?tweeterPhone:phone
            }
            submitLoanMsg(params).then(res=>{
              if(res.code == 0){
                wx.showToast({title: '提交信息成功',})
                // 清空提交的信息
                this.setData({formMsg:{carName:'',userName:'',userPhone:''}})
              }
            })
          }
        }
      })
    },
    // 输入框双向数据绑定
    bindKeyInput(e) {
      this.setData({ [`${e.currentTarget.dataset.params}`]: e.detail.value })
    },
    // 获取当前用户信息
    setOpenid(){
       getBaseInfo().then(res=>{
          if(res.data.openId){
            this.setData({userInfo:res.data,codeStatus:res.code})
          }
      })
    },
    // 获取电话号码
    getPhoneNumber(e) {
      if (!e.detail.iv) {
        wx.showToast({
          title: '手机号获取失败，请重试',
          icon: 'none',
          duration: 1500,
        })
        return
      }
      let { iv: ivStr, encryptedData } = e.detail
      getUserPhone({ ivStr, encryptedData }).then((res) => {
        if (res.code === 0) {
          this.setData({ ['formMsg.userPhone']: res.data })
        } else {
          wx.showToast({
            title: '手机号获取失败，请重试',
            icon: 'none',
            duration: 1500,
          })
        }
      })
    },
  },
})
